<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Prefilters/Postfilters</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="plugins.html" title="Chapter 18. Extending Smarty With Plugins">
<link rel="prev" href="plugins.compiler.functions.html" title="Compiler Functions">
<link rel="next" href="plugins.outputfilters.html" title="Output Filters">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Prefilters/Postfilters</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="plugins.compiler.functions.html">Prev</a> </td>
<th width="60%" align="center">Chapter 18. Extending Smarty With Plugins</th>
<td width="20%" align="right"> <a accesskey="n" href="plugins.outputfilters.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Prefilters/Postfilters">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="plugins.prefilters.postfilters"></a>Prefilters/Postfilters</h2></div></div></div>
<p>
     Prefilter and postfilter plugins are very similar in concept; where
     they differ is in the execution -- more precisely the time of their
     execution.
    </p>
<div class="funcsynopsis">
<table border="0" summary="Function synopsis" cellspacing="0" cellpadding="0" class="funcprototype-table">
<tr>
<td><code class="funcdef">string <b class="fsfunc">smarty_prefilter_name</b>(</code></td>
<td>
<var class="pdparam">$source</var>, </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<var class="pdparam">$template</var><code>)</code>;</td>
<td> </td>
</tr>
</table>
<div class="paramdef-list">
<code>string <var class="pdparam">$source</var></code>;<br><code>object <var class="pdparam">$template</var></code>;</div>
<div class="funcprototype-spacer"> </div>
</div>
<p>
     Prefilters are used to process the source of the template immediately
     before compilation. The first parameter to the prefilter function is
     the template source, possibly modified by some other prefilters. The
     plugin is supposed to return the modified source.  Note that this
     source is not saved anywhere, it is only used for compilation.
    </p>
<div class="funcsynopsis">
<table border="0" summary="Function synopsis" cellspacing="0" cellpadding="0" class="funcprototype-table">
<tr>
<td><code class="funcdef">string <b class="fsfunc">smarty_postfilter_name</b>(</code></td>
<td>
<var class="pdparam">$compiled</var>, </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<var class="pdparam">$template</var><code>)</code>;</td>
<td> </td>
</tr>
</table>
<div class="paramdef-list">
<code>string <var class="pdparam">$compiled</var></code>;<br><code>object <var class="pdparam">$template</var></code>;</div>
<div class="funcprototype-spacer"> </div>
</div>
<p>
     Postfilters are used to process the compiled output of the template
     (the PHP code) immediately after the compilation is done but before the
     compiled template is saved to the filesystem. The first parameter to
     the postfilter function is the compiled template code, possibly
     modified by other postfilters. The plugin is supposed to return the
     modified version of this code.
    </p>
<div class="example">
<a name="id465164"></a><p class="title"><b>Example 18.7. prefilter plugin</b></p>
<div class="example-contents"><pre class="programlisting">

&lt;?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     prefilter.pre01.php
 * Type:     prefilter
 * Name:     pre01
 * Purpose:  Convert html tags to be lowercase.
 * -------------------------------------------------------------
 */
 function smarty_prefilter_pre01($source, Smarty_Internal_Template $template)
 {
     return preg_replace('!&lt;(\w+)[^&gt;]+&gt;!e', 'strtolower("$1")', $source);
 }
?&gt;

     </pre></div>
</div>
<br class="example-break"><p></p>
<div class="example">
<a name="id465181"></a><p class="title"><b>Example 18.8. postfilter plugin</b></p>
<div class="example-contents"><pre class="programlisting">

&lt;?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     postfilter.post01.php
 * Type:     postfilter
 * Name:     post01
 * Purpose:  Output code that lists all current template vars.
 * -------------------------------------------------------------
 */
 function smarty_postfilter_post01($compiled, Smarty_Internal_Template $template)
 {
     $compiled = "&lt;pre&gt;\n&lt;?php print_r(\$template-&gt;getTemplateVars()); ?&gt;\n&lt;/pre&gt;" . $compiled;
     return $compiled;
 }
?&gt;

     </pre></div>
</div>
<br class="example-break"><p>
     See also
     <a class="link" href="api.register.filter.html" title="registerFilter()">
     <code class="varname">registerFilter()</code></a> and
     <a class="link" href="api.unregister.filter.html" title="unregisterFilter()">
     <code class="varname">unregisterFilter()</code></a>.
    </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="plugins.compiler.functions.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="plugins.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="plugins.outputfilters.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Compiler Functions </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Output Filters</td>
</tr>
</table>
</div>
</body>
</html>
